var express = require('express')
var router = express.Router()
var {qfuser_model,qfanno_model,qfadvise_model,qfxueke_model,qfbanji_model} = require('../db/model') 
var {FindOneDataMore, InsertManyData , UpdateTableData,FindTableData,RemoveTableData} = require('../db')
var axios = require("axios")
const { getToken, checkToken } = require('../utils/token')
var multer = require("multer")

router.get("/test",(req,res)=>{
    res.json({
        code:200,
        msg:"Admin - Admin  测试Api 接口 ",
        result:null
    })
})

// 注册
router.post("/register",(req,res)=>{
    var body = req.body;
    // 先判断再去注册
    FindOneDataMore({
        model:qfuser_model,
        query:{
            $or:[
                {
                    username:body.username 
                },
                {
                    phone:body.phone 
                }
            ]
        },
        res,
        callback(result){
            if(result){
                res.json({
                    code:401,
                    msg:"用户已经存在,请重新注册",
                    result,
                })
            }else{
                body.time  = new Date()
                InsertManyData({
                    model:qfuser_model,
                    data:body,
                    msg:"注册成功",
                    res 
                })
            }
        }
    })
})

// 登录
router.post("/login",(req,res)=>{
    var body  = req.body;
    FindOneDataMore({
        model:qfuser_model,
        query:body, 
        res,
        callback(result){
            if(result){
                // Token  
                var token = getToken(body)  // 用户名和密码得到一个 token 
                res.json({
                    code:200,
                    msg:"登录成功",
                    result,
                    token 
                })
            }else{
                res.json({
                    code:401,
                    msg:"登录失败,用户名或者密码错误",
                    result,
                })
            }
        }
    })
})


// 发送验证码 
router.post("/sendcaptcha",(req,res)=>{
    var body = req.body;
    // 先去查询一下 
    // 再去发送验证码 
    FindOneDataMore({
        model:qfuser_model,
        query:body,
        res,
        callback(result){
            if(result){
                // 发送验证code 
                axios.get("http://47.104.209.44:3333/captcha/sent",{
                    params:body // {phone:xxxxxx}
                })
                .then(({data})=>{
                    console.log(data)
                    if(data.code==200){
                        res.json({
                            code:200,
                            data,
                            msg:'验证码发送成功'
                        })
                    }else{
                        res.json({
                            code:401,
                            data,
                            msg:'验证码发送失败'
                        })
                    }
                    
                })
            }else{
                res.json({
                    code:401,
                    msg:'手机号未注册,请先注册',
                    result 
                })
            }
        }
    })
})

// 校验验证码 
router.post("/chechcaptcha",(req,res)=>{
    var body = req.body;  // phone captcha
    FindOneDataMore({
        model:qfuser_model,
        query:{ phone: body.phone },
        res,
        callback(result){
            if(result){
                // 发送验证code 
                axios.get("http://47.104.209.44:3333/captcha/verify",{
                    params:body // {phone:xxxxxx}
                })
                .then(({data})=>{
                    // console.log(data);
                    // data.code==200
                    if(data.code==200){
                        var token = getToken({
                            username:result.username,
                            password:result.password,
                            phone:result.phone 
                        })
                        res.json({
                            code:200,
                            data,
                            msg:'验证码校验成功',
                            token
                        })
                    }else{
                        res.json({
                            code:401,
                            data,
                            msg:'验证码校验失败'
                        })
                    }
                })
            }else{
                res.json({
                    code:401,
                    msg:'手机号未注册,请先注册',
                    result 
                })
            }
        }
    })
})


// 先确认验证码 再去修改密码 
router.post("/changepwdbyphone",(req,res)=>{
    var body = req.body;  // phone captcha
    FindOneDataMore({
        model:qfuser_model,
        query:{ phone: body.phone },
        res,
        callback(result){
            if(result){
                // 发送验证code 
                axios.get("http://47.104.209.44:3333/captcha/verify",{
                    params:body // {phone:xxxxxx}
                })
                .then(({data})=>{
                    // console.log(data);
                    // data.code==200
                    if(data.code==200){
                        UpdateTableData({
                            model:qfuser_model,
                            query:{
                                phone:body.phone 
                            },
                            data:{
                                password:body.password
                            },
                            res,
                            msg:'重置密码成功,请重新登录',
                        })

                    }else{
                        res.json({
                            code:401,
                            data,
                            msg:'验证码校验失败'
                        })
                    }
                })
            }else{
                res.json({
                    code:401,
                    msg:'手机号未注册,请先注册',
                    result 
                })
            }
        }
    })
})

// 获取个人信息 
router.post("/getusreinfo",(req,res)=>{
    checkToken(req,res,({username})=>{
        FindOneDataMore({
            model:qfuser_model,
            query:{username},
            msg:'获取个人信息成功',
            res 
        })
    })
})

// 上传文件 
const storage = multer.diskStorage({  // 硬盘存储 
    destination: function (req, file, cb) {
      cb(null, './public/images/uploads')
    },
    filename: function (req, file, cb) {
      const uniqueSuffix = "WH2114__"+Date.now() + '-' + Math.round(Math.random() * 1E9)   // 文件名、
      cb(null, file.fieldname + '-' + uniqueSuffix + file.originalname)
    }
})
  
const upload = multer({ storage: storage }).any()

// 上传文件 
router.post("/uploadfile",upload,(req,res)=>{
    var path = req.files[0].path
    res.json({
        code:200,
        msg:"上传成功",
        path,
        result:path 
    })
})




// 修改用户信息 
router.post("/changeuserinfo",(req,res)=>{
    var body = req.body;
    checkToken(req,res,({username})=>{
        UpdateTableData({
            model:qfuser_model,
            query:{username},
            data:{
                $set:body
            },
            res,
            msg:"成功"
        })
    })
})

// 修改密码
router.post("/changepass",(req,res)=>{
    var body = req.body;
    checkToken(req,res,({username})=>{
        FindOneDataMore({
            model:qfuser_model,
            query:{username,password:body.oldpass},
            res,
            callback:result=>{
                if(result){
                    UpdateTableData({
                        model:qfuser_model,
                        query:{username},
                        data:{
                            $set:{
                                password:body.newpass 
                            }
                        },
                        res,
                        msg:"密码修改成功"
                    })
                }else{
                    res.json({
                        code:401,
                        msg:'旧密码错误',
                        result:null 
                    })
                }
            }
        })
    })
})


// 公告新增
router.post("/addanno",(req,res)=>{
    var body = req.body
    body.time = new Date()
    checkToken(req,res,({username})=>{
    InsertManyData({
   model:qfanno_model,
   data:body,
   res,msg:"公告添加成功"
     })
       
    })
  })


  //公告列表

  router.post("/annolist",(req,res)=>{
    var body = req.body; 
    var query = {} 
    var {keyword,type} = body
    if(keyword && type){
        query = {
            title:new RegExp(keyword),
            type:type  * 1 
        }
    }else if(keyword){
        query = {
            $or:[
                {
                    title:new RegExp(keyword),
                },
                {
                    content:new RegExp(keyword),
                }
            ]
        }
    }else if(type){
        query = {
            type:type * 1 
        }
    }
    checkToken(req,res,({username})=>{
        FindTableData({
            model:qfanno_model,
            query:query,
            msg:'成功',
            res 
        })
    })
})

//公告删除


router.post('/annodelete',(req,res)=>{
    var body=req.body
    checkToken(req,res,({username})=>{
        RemoveTableData({
            model:qfanno_model,
            query:{
                _id:body._id
            },
            res,
            msg:'删除成功' 
        })
    })
})


// 查询一条公告 
router.post("/getannoone",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        FindOneDataMore({
            model:qfanno_model,
            query:{
                _id:body._id
            },
            msg:'查询成功',
            res 
        })
    })
})

// 修改公告 
router.post("/updateanno",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        UpdateTableData({
            model:qfanno_model,
            query:{
                _id:body._id
            },
            data:body,
            msg:'修改成功',
            res 
        })
    })
})



// 用户新增
router.post("/adduser",(req,res)=>{
    var body = req.body
    body.time = new Date()
    checkToken(req,res,({username})=>{
    InsertManyData({
   model:qfuser_model,
   data:body,
   res,
   msg:"用户添加成功"
     })
       
    })
  })

  //公告列表

  router.post("/userlist",(req,res)=>{
       checkToken(req,res,({username})=>{
        FindTableData({
            model:qfuser_model,
            query:{},
            msg:'成功',
            res 
        })
    })
})

//用户删除
router.post('/userdelete',(req,res)=>{
    var body=req.body
    checkToken(req,res,({username})=>{
        RemoveTableData({
            model:qfuser_model,
            query:{
                _id:body._id
            },
            res,
            msg:'删除成功' 
        })
    })
})

//查询一条用户
router.post("/getuserone",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        FindOneDataMore({
            model:qfuser_model,
            query:{
                _id:body._id
            },
            msg:'查询成功',
            res 
        })
    })
})


// 修改用户 
router.post("/updateuser",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        UpdateTableData({
            model:qfuser_model,
            query:{
                _id:body._id
            },
            data:body,
            msg:'修改成功',
            res 
        })
    })
})

// 意见新增 
router.post("/addadvise",(req,res)=>{
    var body = req.body; 
    body.time = new Date()
    checkToken(req,res,({username})=>{
        InsertManyData({
            model:qfadvise_model,
            data:body,
            res,
            msg:"意见添加成功"
        })
    })
})

// 意见列表 
router.post("/adviselist",(req,res)=>{
    var body = req.body; 
    var query = {} 
    var {keyword,type} = body
    if(keyword && type){
        query = {
            title:new RegExp(keyword),
            type:type  * 1 
        }
    }else if(keyword){
        query = {
            $or:[
                {
                    title:new RegExp(keyword),
                },
                {
                    content:new RegExp(keyword),
                }
            ]
        }
    }else if(type){
        query = {
            type:type * 1 
        }
    }
    checkToken(req,res,({username})=>{
        FindTableData({
            model:qfadvise_model,
            query:query,
            msg:'成功',
            res 
        })
    })
})

// 意见删除
router.post("/advisedelete",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        RemoveTableData({
            model:qfadvise_model,
            query:{
                _id:body._id
            },
            msg:'删除成功',
            res 
        })
    })
})

// 查询一条意见
router.post("/getadviseone",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        FindOneDataMore({
            model:qfadvise_model,
            query:{
                _id:body._id
            },
            msg:'查询成功',
            res 
        })
    })
})

// 修改意见
router.post("/updateadvise",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        UpdateTableData({
            model:qfadvise_model,
            query:{
                _id:body._id
            },
            data:body,
            msg:'修改成功',
            res 
        })
    })
})

// 学科新增 
router.post("/addxueke",(req,res)=>{
    var body = req.body; 
    body.time = new Date()
    checkToken(req,res,({username})=>{
        InsertManyData({
            model:qfxueke_model,
            data:body,
            res,
            msg:"学科添加成功"
        })
    })
})

// 学科列表 
router.post("/xuekelist",(req,res)=>{
    var body = req.body; 
    var query = {} 
    var {keyword,type} = body
    if(keyword && type){
        query = {
            title:new RegExp(keyword),
            type:type  * 1 
        }
    }else if(keyword){
        query = {
            $or:[
                {
                    title:new RegExp(keyword),
                },
                {
                    content:new RegExp(keyword),
                }
            ]
        }
    }else if(type){
        query = {
            type:type * 1 
        }
    }
    checkToken(req,res,({username})=>{
        FindTableData({
            model:qfxueke_model,
            query:query,
            msg:'成功',
            res 
        })
    })
})

// 学科删除
router.post("/xuekedelete",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        RemoveTableData({
            model:qfxueke_model,
            query:{
                _id:body._id
            },
            msg:'删除成功',
            res 
        })
    })
})

// 查询一条学科
router.post("/getxuekeone",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        FindOneDataMore({
            model:qfxueke_model,
            query:{
                _id:body._id
            },
            msg:'查询成功',
            res 
        })
    })
})

// 修改学科
router.post("/updatexueke",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        UpdateTableData({
            model:qfxueke_model,
            query:{
                _id:body._id
            },
            data:body,
            msg:'修改成功',
            res 
        })
    })
})

//修改权限
router.post("/roleupdate",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        UpdateTableData({
            model:qfuser_model,
            query:{
                _id:body._id
            },
            data:body,
            msg:'修改成功',
            res 
        })
    })
})




// 班级新增 
router.post("/addbanji",(req,res)=>{
    var body = req.body; 
    body.time = new Date()
    checkToken(req,res,({username})=>{
        InsertManyData({
            model:qfbanji_model,
            data:body,
            res,
            msg:"学科添加成功"
        })
    })
})

// 班级列表 
router.post("/banjilist",(req,res)=>{
    var body = req.body; 
    var query = {} 
    var {keyword,type} = body
    if(keyword && type){
        query = {
            title:new RegExp(keyword),
            type:type  * 1 
        }
    }else if(keyword){
        query = {
            $or:[
                {
                    title:new RegExp(keyword),
                },
                {
                    content:new RegExp(keyword),
                }
            ]
        }
    }else if(type){
        query = {
            type:type * 1 
        }
    }
    checkToken(req,res,({username})=>{
        FindTableData({
            model:qfbanji_model,
            query:query,
            msg:'成功',
            res 
        })
    })
})

// 班级删除
router.post("/banjidelete",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        RemoveTableData({
            model:qfbanji_model,
            query:{
                _id:body._id
            },
            msg:'删除成功',
            res 
        })
    })
})

// 查询一条班级
router.post("/getbanjione",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        FindOneDataMore({
            model:qfbanji_model,
            query:{
                _id:body._id
            },
            msg:'查询成功',
            res 
        })
    })
})

// 修改班级
router.post("/updatebanji",(req,res)=>{
    var body = req.body; 
    checkToken(req,res,({username})=>{
        UpdateTableData({
            model:qfbanji_model,
            query:{
                _id:body._id
            },
            data:body,
            msg:'修改成功',
            res 
        })
    })
})



module.exports = router 